<?php
namespace app\shop\controller;
use app\AdminRest;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Cap;
use app\shop\model\Date;
use app\shop\model\OrderAddress;
use app\shop\model\OrderGoods;
use app\shop\model\RefundOrder;
use app\shop\model\Wallet;
use longbingcore\wxcore\Excel;
use think\App;
use app\shop\model\Order as Model;


class AdminExcel extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->order_goods_model = new OrderGoods();

        $this->refund_order_model = new RefundOrder();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];
        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['a.create_time','between',"$start_time,$end_time"];
        }
        //商品名字搜索
        if(!empty($input['goods_name'])){

            $dis[] = ['c.goods_name','like','%'.$input['goods_name'].'%'];
        }
        //手机号搜索
        if(!empty($input['mobile'])){

            $dis[] = ['d.mobile','like','%'.$input['mobile'].'%'];
        }
        //订单状态搜索
        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];
        }
        //店铺名字搜索
        if(!empty($input['store_id'])){

            $dis[] = ['a.cap_id','=',$input['store_id']];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        $data = $this->model->adminDataSelect($dis,$input['limit']);

        $name  = '订单列表';

        $header=[
            '订单ID',
            '商品名',
            '商品价格',
            '商品规格',
            '商品数量',
            '下单人',
            '手机号',
            '实收金额',
            '系统订单号',
            '付款订单号',
            '下单时间',
            '支付方式',
            '配送方式',
            '核销人',
            '状态',
        ];

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['goods_name'];

            $info[] = $v['goods_price'];

            $info[] = $v['spe_name'];

            $info[] = $v['goods_num'];

            $info[] = $v['store_name'];

            $info[] = $v['name'];

            $info[] = $v['user_name'];

            $info[] = $v['mobile'];

            $info[] = $v['pay_price'];

            $info[] = $v['order_code'];

            $info[] = $v['transaction_id'];

            $info[] = $v['create_time']?date('Y-m-d H:i:s',$v['create_time']):'暂无信息';

            $info[] = !empty($v['balance'])?'余额支付':'微信支付';

            $info[] = $v['send_type']==1?'自提':'快递';

            $info[] = $v['send_type']==1?'自提':'快递';

            $info[] = $this->orderStatusText($v['pay_type']);

            $info[] = $v['hx_user_name'];

            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data,'',2);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-30 16:32
     * @功能说明:
     */
    public function orderStatusText($status){

        switch ($status){

            case 1:
                return '待支付';

                break;
            case 2:
                return '已支付';

                break;

            case 7:
                return '已完成';

                break;

            case -1:
                return '已取消';

                break;

        }

    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-18 13:37
     * @功能说明:财务数据统计导出
     */
    public function dateCount(){

        $input = $this->_param;

        $cap_id = $input['cap_id'];

        $date_model   = new Date();

        $wallet_model = new Wallet();

        $cap_model    = new Cap();

        $date_model->dataInit($this->_uniacid);

        $dis[] = ['uniacid','=',$this->_uniacid];
        //时间搜素
        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['date_str','between',"$start_time,$end_time"];
        }

        $date_list = $date_model->dataList($dis,100000);
        //店铺名字
        $store_name = $cap_model->where(['id'=>$cap_id])->value('store_name');
        //开始时间结束时间
        if(!empty($start_time)){

            $date_list['start_time'] = $start_time;

            $date_list['end_time']   = $end_time;

        }else{

            $date_list['start_time'] = $date_model->where(['uniacid'=>$this->_uniacid])->min('date_str');

            $date_list['end_time']   = $date_model->where(['uniacid'=>$this->_uniacid])->max('date_str');

        }

        if(!empty($date_list['data'])){

            foreach ($date_list['data'] as $k=>$v){
                //订单金额
                $date_list['data'][$k]['order_price']  = $this->model->datePrice($v['date_str'],$this->_uniacid,$cap_id);
                //退款金额
                $date_list['data'][$k]['refund_price'] = $this->refund_order_model->datePrice($v['date_str'],$this->_uniacid,$cap_id);
                //提现金额
                $date_list['data'][$k]['wallet_price'] = $wallet_model->datePrice($v['date_str'],$this->_uniacid,$cap_id);

            }

        }

        $name  = $store_name.'财务报表';

        $header=[
            '收支时间',
            '订单收入',
            '订单退款',
            '提现（元）',
        ];

        $new_data = [];

        foreach ($date_list['data'] as $v){

            $info   = array();

            $info[] = $v['date'];

            $info[] = $v['order_price'];

            $info[] = $v['refund_price'];

            $info[] = $v['wallet_price'];

            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($date_list);

    }








}
